package com.erik.lin.controller;

import com.erik.lin.annotion.MapResult;
import com.erik.lin.orm.dao.DacpDataexCryptMapper;
import com.erik.lin.orm.po.DacpDataexCrypt;
import com.erik.lin.orm.po.DacpDataexCryptExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Erik on 2017/9/5.
 */
@Controller
@RequestMapping("/cache")
public class CacheTestController {

    Logger LOG = LoggerFactory.getLogger(CacheTestController.class);

    @Autowired
    DacpDataexCryptMapper dacpDataexCryptMapper;

    @RequestMapping(value = "/select/{id}", method = RequestMethod.GET)
    @MapResult
    @Cacheable(value = "ehRedisCache", key = "#id")
    public @ResponseBody Map<String, Object> testSelect(@PathVariable("id") String id) {
        Map<String, Object> rs = new HashMap<>();
        DacpDataexCrypt dacpDataexCrypt = dacpDataexCryptMapper.selectByPrimaryKey(id);
        LOG.info("--------------------daoCrypt-----------------------");
        rs.put("crypt", dacpDataexCrypt);
        return rs;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @MapResult
    @CacheEvict(value = "ehRedisCache", key = "args[0].cryptId")
    public @ResponseBody Map<String, Object> testUpdate(@RequestBody DacpDataexCrypt crypt) {
        Map<String, Object> rs = new HashMap<>();
        dacpDataexCryptMapper.updateByPrimaryKey(crypt);
        return rs;
    }
}
